#include <pcap.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "recv_init.h"
#include "log.h"
#include "socket_init.h"
#include "pcap_init.h"

//the value return by recv_init has many meannings 
//0:pcap init fail
//1:pcap init success
//2:socket init fail
//3:socket init success
//4:the mode that not define

extern struct core_conf core_conf;

int recv_init()
{
	int mode=core_conf.pcap_mode;

	pcap_t *pcap_handle = NULL;
	int s;
	
	if(mode==0 || mode==1)
	{
		log_info("begin to init the pcap");

		//get_task_by_name("socket_task")->is_default=0;

		pcap_handle = pcap_init(core_conf.pcap_mode,core_conf.pcap_file,core_conf.pcap_interface);

		if(pcap_handle==NULL)
		{
			log_info("the pcap_init fail");
			return 0;
		}
		else
		{
			core_conf.pcap_handle=pcap_handle;

			log_info("the pcap_init success");
			return 1;
		}
	}

	else if (mode ==2)
	{
		log_info("begin to init the socket");	
		
		//get_task_by_name("pcap_task")->is_default=0;
		
		struct sockaddr_in *addr;

		addr=(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));

		s = socket_init(core_conf.server_conf->port,core_conf.server_conf->ip,addr);

			
		if(s<0)
		{
			log_info("the socket_init fail");
			return 2;
		}
		else
		{
			core_conf.s=s;
			core_conf.addr=addr;
			log_info("the socket_init success");
			return 3;
		}
	}
	else
	{

		log_info("the mode that is not defined");
		return 4;
	}
	
	return 5;	
}
